package com.adcc.swingui.ui;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.plaf.PanelUI;

/**
 * 界面圆边UI
 *
 * @author illusion <anymorekvetch@gmail.com>
 */
public class RoundSidePanelUI extends PanelUI {

//    private static final Color DEFAULT_BG_COLOR = new Color(216, 216, 216, 100);
    private static final Color DEFAULT_BG_COLOR = new Color(175, 222, 247, 0);
    private static final Color DEFAULT_LINE_COLOR = new Color(100, 100, 100, 100);
    private Color bgColor;
    private Color lineColor;
    private int roundRect;

    public Color getBgColor() {
        return bgColor;
    }

    public void setBgColor(Color bgColor) {
        this.bgColor = bgColor;
    }

    public Color getLineColor() {
        return lineColor;
    }

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }

    public int getRoundRect() {
        return roundRect;
    }

    public void setRoundRect(int roundRect) {
        this.roundRect = roundRect;
    }

    public RoundSidePanelUI() {
        this.setRoundRect(20);
        this.setBgColor(DEFAULT_BG_COLOR);
        this.setLineColor(DEFAULT_LINE_COLOR);
    }

    public RoundSidePanelUI(int roundRect, Color bgColor, Color lineColor) {
        this.setRoundRect(roundRect);
        this.setBgColor(bgColor);
        this.setLineColor(lineColor);
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        Dimension d = c.getSize();
        Insets ins = c.getInsets();
        Graphics2D g2 = (Graphics2D) g;
        setQuanlifiedGraphics(g2);
        g2.setColor(this.getBgColor());
        g2.fillRoundRect(ins.left, ins.top, d.width - ins.left - ins.right, d.height - ins.top - ins.bottom,
                this.roundRect, this.roundRect);
        g2.setColor(this.getLineColor());
        g2.fillRoundRect(ins.left, ins.top, d.width - ins.left - ins.right - 1,
                d.height - ins.top - ins.bottom - 1, this.roundRect, this.roundRect);
        super.paint(g2, c);
    }

    @Override
    public void installUI(JComponent c) {
        c.setOpaque(false);
        super.installUI(c);
    }

    /**
     * 开启高质量图像渲染策略
     *
     * @param g2
     * @return
     */
    private RenderingHints setQuanlifiedGraphics(Graphics2D g2) {
        RenderingHints hints = g2.getRenderingHints();
        RenderingHints oldHints = (RenderingHints) hints.clone();
        hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        g2.setRenderingHints(hints);
        return oldHints;
    }
}
